package com.kim.rssreader.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import cn.waps.AppConnect;

import com.kim.rssreader.R;
import com.kim.rssreader.colorpicker.ColorPickerDialog;
import com.kim.rssreader.colorpicker.ColorPickerDialog.OnColorChangedListener;
import com.kim.rssreader.utils.Utils;
import com.kim.rssreader.utils.ValueConstant;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.controller.RequestType;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;

public class Settings extends PreferenceActivity implements
		OnPreferenceClickListener {

	ImageView back;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_settings);
		addPreferencesFromResource(R.xml.preferences);
		initView();
		System.runFinalization();
		System.gc();
	}

	private void initView() {
		back = (ImageView) findViewById(R.id.img_back);
		back.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Settings.this.finish();
			}
		});

		/**
		 * 检查更新项
		 */
//		Preference preferenceCheckUpdate = findPreference(getResources()
//				.getString(R.string.checkupdate_key));
//		preferenceCheckUpdate.setOnPreferenceClickListener(this);

		/**
		 * 用户反馈项
		 */
		Preference preferenceResponse = findPreference(getResources()
				.getString(R.string.response_key));
		preferenceResponse.setOnPreferenceClickListener(this);

		/**
		 * 关于我们
		 */
		Preference preferenceAboutus = findPreference(getResources().getString(
				R.string.about_us_key));
		preferenceAboutus.setOnPreferenceClickListener(this);
		
		/**
		 * 分享
		 */
//		Preference preferenceShare = findPreference(getResources().getString(
//				R.string.share_key));
//		preferenceShare.setOnPreferenceClickListener(this);

		// Preference preference = findPreference("paintcolor");
		// preference
		// .setOnPreferenceClickListener(new OnPreferenceClickListener() {
		//
		// @Override
		// public boolean onPreferenceClick(Preference preference) {
		// showDialog(ValueConstant.COLOR_PICKER_DIALOG);
		// return false;
		// }
		// });
	}

	@Override
	public boolean onPreferenceClick(Preference preference) {
		/*if (getResources().getString(R.string.checkupdate_key).equals(
				preference.getKey())) {
			// 检查更新
			// 万普
			AppConnect.getInstance(this).checkUpdate(this);
			// 有米
			// YoumiPush.update(this);
			return true;
		} else */if (getResources().getString(R.string.response_key).equals(
				preference.getKey())) {
			// 用户反馈
			// 万普
			AppConnect.getInstance(this).showFeedback();
			return true;
		} else if (getResources().getString(R.string.about_us_key).equals(
				preference.getKey())) {
			Intent intent = new Intent();
			intent.setClass(this, AboutActivity.class);
			startActivity(intent);
			return true;
		}
//		else if (getResources().getString(R.string.share_key).equals(//分享
//				preference.getKey())) {
//			Toast.makeText( this, "test", Toast.LENGTH_SHORT).show();
//			UMSocialService controller = UMServiceFactory.getUMSocialService("test", RequestType.SOCIAL);
//			controller.setShareContent("hello hello hello");
//			controller.openShare( this ,false);
//			
//			UMServiceFactory.shareTo( this, SHARE_MEDIA.TENCENT, "我使用了快速分享接口（UMServiceFactory.share）", null);		//只打开TX微博
//			return true;
//		}
		return false;
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case ValueConstant.COLOR_PICKER_DIALOG:
			return new ColorPickerDialog(this, "ColorPicker",
					new OnColorChangedListener() {

						@Override
						public void colorChanged(int color) {
							Utils.toastShort(Settings.this, "PickedColor:"
									+ color);
						}
					});
		default:
			break;
		}
		return super.onCreateDialog(id);
	}

}
